package com.videoaggre.zcl.ui.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import androidx.annotation.StringRes
import androidx.recyclerview.widget.RecyclerView
import androidx.viewbinding.ViewBinding
import com.videoaggre.zcl.App
import com.videoaggre.zcl.R
import com.videoaggre.zcl.utils.Logger

/**
 * Created by zhoucl on 2019/7/8 0008.
 */
typealias OnClickAdapterListenter = ((View, Int) -> Unit)

abstract class BaseRecyclerAdapter<VH : RecyclerView.ViewHolder, B>(datas: MutableList<B> = mutableListOf()) : RecyclerView.Adapter<VH>() {
    protected var mOnClickListener: ((View) -> Unit)? = null
    protected var mOnClickAdapterListenter: ((View, position: Int) -> Unit)? = null
    var datas: MutableList<B> = datas
        set(value) {
            newDatas(value)
            field = value
        }

    open fun newDatas(datas: MutableList<B>) {

    }

    override fun getItemViewType(position: Int) = position
    fun getItem(position: Int) = datas[position]

    fun clear() {
        datas.let {
            it.clear()
            notifyDataSetChanged()
        }
    }

    open fun remove(position: Int) {
        datas.let {
            if (position < itemCount) {
                it.removeAt(position)
                notifyItemRemoved(position)
                notifyItemRangeChanged(position, itemCount - position)
            }
        }
    }

    fun addAll(addDatas: List<B>?) {
        datas.let {
            if (addDatas != null && !addDatas.isEmpty()) {
                val itemCount = itemCount
                it.addAll(addDatas)
                notifyItemRangeInserted(itemCount, getItemCount())
            }
        }
    }

    fun add(item: B) {
        datas.let {
            it.add(item)
            notifyItemInserted(itemCount - 1)
        }
    }

    fun deleteData(bean: B) {}

    fun isContainPosition(v: View): Boolean {
        return try {
            var tag = v.tag
            if (tag == null) tag = v.getTag(R.id.key_position)
            val i = tag as Int
            i in 0 until itemCount
        } catch (e: Exception) {
            Logger.e(e)
            false
        }
    }

    fun getPositionByView(v: View): Int {
        var tag = v.tag
        if (tag == null) tag = v.getTag(R.id.key_position)
        return tag as Int
    }

    open fun onClickAdapter(v: View) {
        if (isContainPosition(v)) {
            val position = getPositionByView(v)
            subOnClickAdapter(v, position)
            mOnClickAdapterListenter?.invoke(v, position)
        }
    }

    open fun subOnClickAdapter(v: View, position: Int) {

    }

    override fun onBindViewHolder(holder: VH, position: Int) {
        onBindViewHolder(holder, getItem(position), position)
    }

    override fun onBindViewHolder(holder: VH, position: Int, payloads: MutableList<Any>) {
        super.onBindViewHolder(holder, position, payloads)
        onBindViewHolder(holder, getItem(position), position, if (payloads.isNotEmpty() && payloads.size > 0) payloads[0] else null)
    }

    abstract fun onBindViewHolder(holder: VH, bean: B, position: Int)

    open fun onBindViewHolder(holder: VH, bean: B, position: Int, payload: Any?) {

    }

    override fun getItemCount() = datas.size

    fun getString(@StringRes resId: Int, vararg formatArgs: Any?) = App.app.getString(resId, *formatArgs)

    fun getString(@StringRes resId: Int) = App.app.getString(resId)

    fun setOnClickListener(mOnClickListener: ((View) -> Unit)? = null) {
        this.mOnClickListener = mOnClickListener
    }

    fun setOnClickAdapterListenter(mOnClickAdapterListenter: OnClickAdapterListenter? = null) {
        this.mOnClickAdapterListenter = mOnClickAdapterListenter
    }

    fun setViewClick(position: Int, vararg views: View) {
        if (views.isNotEmpty()) {
            for (view in views) {
                view.tag = position
                view.setOnClickListener { v: View -> onClickAdapter(v) }
            }
        }
    }

    fun setViewClick(view: View) {
        view.setOnClickListener { v: View -> onClickAdapter(v) }
    }

    fun setViewClickValue(value: Int, vararg views: View) {
        if (views.isNotEmpty()) {
            for (view in views) {
                view.setTag(R.id.key_value, value)
                view.setOnClickListener { v: View -> onClickAdapter(v) }
            }
        }
    }

    fun getLayoutInflate(content: Context) = LayoutInflater.from(content)
}

class BaseViewHolder<VB : ViewBinding>(var mViewBinding: VB) : RecyclerView.ViewHolder(mViewBinding.root)